﻿namespace AllBots.ViewModel
{
    using System.Windows.Input;
    using BotModels;
    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Command;
    using System.Linq;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using GalaSoft.MvvmLight.Messaging;
    using AllBots.Messaging;
    using Ninject;
    using System.Windows;

    public class LibraryViewModel : ViewModelBase
    {
        #region Private Fields

        private LibraryDataContext context = new LibraryDataContext();
        private ILibraryRepository repository;

        #endregion

        #region INPC

        public const string BotsPropertyName = "Bots";
        private ObservableCollection<Bot> bots = new ObservableCollection<Bot>();
        public ObservableCollection<Bot> Bots
        {
            get
            {
                return bots;
            }

            set
            {
                if (bots == value)
                {
                    return;
                }

                var oldValue = bots;
                bots = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(BotsPropertyName);
            }
        }

        public const string IdSentPropertyName = "IdSent";
        private bool idSent = false;
        public bool IdSent
        {
            get
            {
                return idSent;
            }

            set
            {
                if (idSent == value)
                {
                    return;
                }

                var oldValue = idSent;
                idSent = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(IdSentPropertyName);
            }
        }
        
        #endregion

        #region Commands

        private RelayCommand<Bot> addCommand;
        public ICommand AddCommand
        {
            get
            {
                if (addCommand == null)
                {
                    addCommand = new RelayCommand<Bot>(AddBot);
                }

                return addCommand;
            }
        }

        private RelayCommand<Bot> deleteCommand;
        public ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new RelayCommand<Bot>(DeleteBot);
                }

                return deleteCommand;
            }
        }

        private RelayCommand<Bot> editCommand;
        public ICommand EditCommand
        {
            get
            {
                if (editCommand == null)
                {
                    editCommand = new RelayCommand<Bot>(EditBot);
                }

                return editCommand;
            }
        }

        private RelayCommand<Bot> chatWithBotCommand;
        public ICommand ChatWithBotCommand
        {
            get
            {
                if (chatWithBotCommand == null)
                {
                    chatWithBotCommand =
                        new RelayCommand<Bot>(
                            bot =>
                            {
                                IdSent = true;
                                Messenger.Default.Send<GenericMessage<Bot>>(new GenericMessage<Bot>(bot.Clone<Bot>()));
                                IdSent = false;
                            });
                }

                return chatWithBotCommand;
            }
        }

        #endregion

        public LibraryViewModel()
        {
            repository = IoC.IoCContainer.Get<ILibraryRepository>();
            Messenger.Default.Register<BotAddedMessage<Bot>>(this, BotAdded);
            LoadLibrary();
        }

        private void BotAdded(BotAddedMessage<Bot> b)
        {
            Bots.Add(b.AddedBot);
        }

        private void DeleteBot(Bot b)
        {
            if (repository.DeleteBot(b))
            {
                Bots.Remove(b);
            }
        }

        private void EditBot(Bot b)
        {
            MessageBox.Show("Soon...");
        }

        private void AddBot(Bot b)
        {
            if (repository.AddBot(b))
            {
                Bots.Add(b);
            }
        }

        private void LoadLibrary()
        {
            Bots = new ObservableCollection<Bot>(repository.GetAllBots());
        }
    }
}
